
package com.ibeeking.found.open.b.rest.controller.v1;

import com.alibaba.fastjson.JSONObject;
import com.ibeeking.found.common.annotation.IgnoreResult;
import com.ibeeking.found.open.b.rest.service.IAliDingTalkOpenEventService;
import com.ibeeking.found.open.service.common.param.QueryOpenAuthInfoParam;
import com.ibeeking.found.open.service.common.vo.OpenAuthViewVO;
import com.ibeeking.found.open.service.common.vo.UrlVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.log.utils.LogUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName AliPayOpenEventController
 * @Description 微信主动调用
 * @Author ibeeking
 * @Date 2021-06-10 16:23
 **/

@RestController
@RequestMapping("/v1/open/api/aliDingTalkOpenEvent")
public class AliDingTalkOpenEventController {

    @Resource
    private IAliDingTalkOpenEventService aliDingTalkOpenEventServiceImpl;

    /**
     * 开放平台授权事件
     *
     * @param body 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "开放平台事件消息")
    @PostMapping("/msgEvent")
    @IgnoreResult
    public Object msgEvent(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("signature") String signature,
                           @RequestBody(required = false) JSONObject body
    ) {
        String params = "signature:" + signature + " timestamp:" + timestamp + " nonce:" + nonce + " body:" + body;
        LogUtils.info("aliDtOpenEvent callback params: {}", params);
        return aliDingTalkOpenEventServiceImpl.msgEvent(request, response, timestamp, nonce, signature, body);
    }


    /**
     * 授权注册页面扫码授权   显示二维码
     *
     * @param request
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取企业微信授权页面url")
    @PostMapping(value = "/getDingTalkTpPreAuthUrl")
    public UrlVO getDingTalkTpPreAuthUrl(HttpServletRequest request) throws Exception {
        return aliDingTalkOpenEventServiceImpl.getDingTalkTpPreAuthUrl(request);
    }

    /**
     * 获取永久授权码
     *
     * @param param
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取永久授权码")
    @PostMapping(value = "/getDingTalkTpPermanentAuthInfo")
    public OpenAuthViewVO getDingTalkTpPermanentAuthInfo(@RequestBody @Validated QueryOpenAuthInfoParam param) throws Exception {
        return aliDingTalkOpenEventServiceImpl.getDingTalkTpPermanentAuthInfo(param);
    }
}

